package com.huobao.yunying.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;

/**
 * 检查权限的工具类
 * <p/>
 * Created by wangchenlong on 16/1/26.
 */
public class PermissionsChecker {

    public static final String PERMISSION_TEL = Manifest.permission.READ_PHONE_STATE;
    public static final String PERMISSION_ADDRESS = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String PERMISSION_SDCARD = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    public static final String PERMISSION_2 = Manifest.permission.CAMERA;
    public static final String PERMISSION_1 = Manifest.permission.RECORD_AUDIO;


    // 判断是否缺少权限
    public static boolean lacksPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) ==
                PackageManager.PERMISSION_DENIED;
    }


    // 显示缺失权限提示
    public static  void showMissingPermissionDialog(final Activity act, final String permission, String msg) {
//        AlertDialog.Builder builder = new AlertDialog.Builder(act);
//        builder.setTitle(R.string.help);
//        builder.setMessage(msg);
//
//        // 拒绝, 退出应用
//        builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {
//            @Override public void onClick(DialogInterface dialog, int which) {
//                ExitUtil.getInstant().exitApp(act);
//            }
//        });
//
//        builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
//            @Override public void onClick(DialogInterface dialog, int which) {
//                String [] permissions = new String[]{permission};
//                ActivityCompat.requestPermissions(act, permissions, 200);
//                dialog.dismiss();
//            }
//        });
//
//        builder.setCancelable(false);
//        builder.create().show();
    }
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
    // 启动应用的设置
    private void startAppSettings(Activity act) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + act.getPackageName()));
        act.startActivity(intent);
    }
}
